package com.woparker.kiosk.view;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.woparker.kiosk.R;



public class EmptyDataLayout extends FrameLayout {
    private ImageView mImageView;
    private TextView mMessageView;

    private int mImage;
    private String mMessage;
    private OnClickListener mReinputClickListener;
    private OnClickListener mRetryClickListener;

    public EmptyDataLayout(@NonNull Context context) {
        super(context);
    }

    public EmptyDataLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public void show() {
        removeAllViews();

        View layout = LayoutInflater.from(getContext()).inflate(R.layout.view_empty_data, this, false);
        mImageView = (ImageView) layout.findViewById(R.id.imageView);
        mMessageView = (TextView) layout.findViewById(R.id.message);

        if (mImage == 0) {
            mImage = R.mipmap.sorry;
        }
        mImageView.setImageResource(mImage);

        if (TextUtils.isEmpty(mMessage)) {
            mMessage = getContext().getString(R.string.data_not_found);
        }
        mMessageView.setText(mMessage);

        if (mReinputClickListener != null) {
            layout.findViewById(R.id.reinput).setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    mReinputClickListener.onClick(view);
                }
            });
        }

        if (mRetryClickListener != null) {
            layout.findViewById(R.id.retry).setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    mRetryClickListener.onClick(view);
                }
            });
        }

        addView(layout);
        setVisibility(VISIBLE);
    }

    public void hide() {
        setVisibility(GONE);
    }

    public void setImage(int image) {
        mImage = image;
    }

    public void setMessage(String message) {
        mMessage = message;
    }

    public void setReinputClickListener(OnClickListener reinputClickListener) {
        mReinputClickListener = reinputClickListener;
    }

    public void setRetryClickListener(OnClickListener retryClickListener) {
        mRetryClickListener = retryClickListener;
    }
}
